<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<script language=javascript>
		var i=0;
		var j=1;
		function addEps(){
			if(navigator.appName=="Microsoft Internet Explorer"){
			var addEsps=document.getElementById("addEsps").value;
			for(var m=0;m<addEsps;m++){
				i=i+1;
				if (parent.hasScene == 0 ) //HasEps HasNotScene
				{
					var oTbody=document.getElementById("table1");
					var oRow=oTbody.insertRow();
					var oCell=oRow.insertCell();
					oCell.innerHTML+="<input name='esptext' type='text' value='"+i+"'><input type='radio' name='esp'><bean:message key='checktoaddscene'/>";
					oCell=oRow.insertCell();
					oCell.innerHTML+="";
					oCell=oRow.insertCell();
					oCell.innerHTML+="<input name='shottext' type='text' value='' onblur='isChinese(this);'>";
				}
				else
				{
					j=1;
					var oTbody=document.getElementById("table1");
					var oRow=oTbody.insertRow();
					var oCell=oRow.insertCell();
					oCell.innerHTML+="<input name='esptext' type='text' value='"+i+"'><input type='radio' name='esp'><bean:message key='checktoaddscene'/>";
					oCell=oRow.insertCell();
					oCell.innerHTML+="<input name='scencetext' type='text' value='"+j+"'>";
					oCell=oRow.insertCell();
					oCell.innerHTML+="<input name='shottext' type='text' value='' onblur='isChinese(this);'>";
				}
			}
		}else{
			var addEsps=document.getElementById("addEsps").value;
			for(var m=0;m<addEsps;m++){
				i=i+1;
				if (parent.hasScene == 0 ) //HasEps HasNotScene
				{
					document.getElementById("table1").innerHTML+="<tr><td><input name='esptext' type='text' value='"+i+"'><input type='radio' name='esp'><bean:message key='checktoaddscene'/></td><td></td><td><input name='shottext' type='text' value='' onblur='isChinese(this);'></td></tr>";
				}
				else
				{
					j=1;
					document.getElementById("table1").innerHTML+="<tr><td><input name='esptext' type='text' value='"+i+"'><input type='radio' name='esp'><bean:message key='checktoaddscene'/></td><td><input name='scencetext' type='text' value='"+j+"'></td><td><input name='shottext' type='text' value='' onblur='isChinese(this);'></td></tr>";
				}
			}
		}
			resetRowId();
		}
		function addScene(){
			var addSences=document.getElementById("addScenes").value;
			if(navigator.appName=="Microsoft Internet Explorer"){
				if(parent.hasEps==1){
					if(confirmNoEsp()){alert("<bean:message key='err.inputepsfirst'/>!");return }
					if(confirmNoSelEsp()){alert("<bean:message key='err.selectepsfirst'/>!");return }
					j=2;
					for(var m=0;m<addSences;m++){ 
						var oTbody=document.getElementById("table1");
						var oRow=oTbody.insertRow();
						var oCell=oRow.insertCell();
						oCell.innerHTML+="<td></td>";
						oCell=oRow.insertCell();
						oCell.innerHTML+="<input name='scencetext' type='text' value='"+j+"'><input type='button' value='<bean:message key="delete"/>' onclick='delScene(this);'>";
						oCell=oRow.insertCell();
						oCell.innerHTML+="<input type='text' value=''>";				
						var selectRow=getRowIdFromRadio();
						var rows=oTbody.rows;
						oTable.moveRow(rows.length-1,selectRow+m);
						j++;
					}
				}else{
					for(var m=0;m<addSences;m++){
						var oTbody=document.getElementById("table1");
						var oRow=oTbody.insertRow();
						var oCell=oRow.insertCell();
						oCell.innerHTML+="<td></td>";
						oCell=oRow.insertCell();
						oCell.innerHTML+="<input type='text' value='"+j+"'><input type='button' value='<bean:message key="delete"/>' onclick='delScene(this);'>";
						oCell=oRow.insertCell();
						oCell.innerHTML+="<input type='text' value=''>";
						j++;
					}
				}
			}else{
				if(parent.hasEps==1){
					if(confirmNoEsp()){alert("<bean:message key='err.inputepsfirst'/>!");return }
					if(confirmNoSelEsp()){alert("<bean:message key='err.selectepsfirst'/>!");return }
					j=2;
					var tbody=document.getElementById("table1");
					for(var m=0;m<addSences;m++){
						var newrow=document.createElement("tr");
						var firsttd=document.createElement("td");
						firsttd.innerHTML+="";
						var sectd=document.createElement("td");
						sectd.innerHTML+="<input type='text' value='"+j+"'><input type='button' value='<bean:message key="delete"/>' onclick='delScene(this);'>";
						var thirdtd=document.createElement("td");
						thirdtd.innerHTML+="<input type='text' value=''>";
						newrow.appendChild(firsttd);
						newrow.appendChild(sectd);
						newrow.appendChild(thirdtd);
						var currentrow=document.getElementById(getRowIdFromRadio()+1);
						tbody.insertBefore(newrow,currentrow);
						j++;
					}			
				}else{
					var tbody=document.getElementById("table1");
						for(var m=0;m<addSences;m++){
							var newrow=document.createElement("tr");
							var firsttd=document.createElement("td");
							firsttd.innerHTML+="";
							var sectd=document.createElement("td");
							sectd.innerHTML+="<input type='text' value='"+j+"'><input type='button' value='<bean:message key="delete"/>' onclick='delScene(this);'>";
							var thirdtd=document.createElement("td");
							thirdtd.innerHTML+="<input type='text' value=''>";
							newrow.appendChild(firsttd);
							newrow.appendChild(sectd);
							newrow.appendChild(thirdtd);
							tbody.appendChild(newrow);
							j++;
					}
				}
			}
			resetRowId();
		}
		function addShot(){
			var m=document.getElementById("addShots").value;
			if(navigator.appName=="Microsoft Internet Explorer"){
				var tbody=document.getElementById("table1");
				var newrow=tbody.insertRow();
				var cell1=newrow.insertCell();
				cell1.innerHTML+="";
				var cell2=newrow.insertCell();
				cell2.innerHTML+="";
				var cell3=newrow.insertCell();
				cell3.innerHTML+="<input type='text' value='"+m+"'>";
			}else{
				document.getElementById("table1").innerHTML+="<tr><td></td><td></td><td><input type='text' value='"+m+"'></td></tr>";
			}
			resetRowId();
		}

		function getRowIdFromRadio(){
			var radios=document.getElementsByName("esp");
			for(var i=0;i<radios.length;i++){
				if(radios[i].checked){
					var num=parseInt(radios[i].parentNode.parentNode.getAttribute("id"),10);
					return num;
				}
			}
		}
		function resetRowId(){
			var trs=document.getElementsByTagName("tr");
			for(var i=0;i<trs.length;i++){
				trs[i].setAttribute("id",i+1);
			}
		}
		function confirmNoEsp(){
			var radios=document.getElementsByName("esp");
			if(top.hasEps == 1 && radios.length==0){
				return true;
			}
		}
		function confirmNoSelEsp(){
			var radios=document.getElementsByName("esp");
			var result=true;
			for(var i=0;i<radios.length;i++){
				if(top.hasEps == 1 && radios[i].checked){
					result=false;
				}
			}
			return result;
		}
		function setHasEsp(){
			var espCheck=parent.document.getElementById("hasEspCheck");
			if(espCheck.checked){
				parent.hasEps = 1;
				var ename=parent.document.getElementById("espName");
				ename.readOnly=false;
				document.getElementById("addEspButtion").innerHTML="<input type='button' value='<bean:message key="episode.add"/>' onclick='addEps();'><input id='addEsps' type='text' value='1'>";
			}else{
				parent.hasEps = 0;
				var ename=parent.document.getElementById("espName");
				ename.value="";
				ename.readOnly=true;
				document.getElementById("addEspButtion").innerHTML="";
			}
		}
		function setHasScence(){
			var scenceCheck=parent.document.getElementById("hasScenceCheck");
			if(scenceCheck.checked){
				parent.hasScene = 1;
				var sname=parent.document.getElementById("scenceName");
				sname.readOnly=false;
				document.getElementById("addScenceButtion").innerHTML="<input type='button' value='<bean:message key="scene.add"/>' onclick='addScene();'><input id='addScenes' type='text' value='1'>";
			}else{
				parent.hasScene = 0;
				var sname=parent.document.getElementById("scenceName");
				sname.value="";
				sname.readOnly=true;
				document.getElementById("addScenceButtion").innerHTML="";
			}
		}
		function setHasShot(){
			var espCheck=parent.document.getElementById("hasEspCheck");
			var scenceCheck=parent.document.getElementById("hasScenceCheck");
			if(scenceCheck.checked || espCheck.checked){
				document.getElementById("addShotButton").innerHTML="";
			}else{
				document.getElementById("addShotButton").innerHTML='<input type="button" value="<bean:message key='shot.add'/>" onclick="addShot();"><input id="addShots" type="text" value="1">';
			}
		}
		function getEsps(){
			if(parent.hasEps==0){
				return "";
			}
			var a=document.getElementsByName("esptext");
			var b="";
			for(var i=0;i<a.length;i++){
				b+=a[i].value+";";
			}
			return b;
		}
		function getScences(){
			var trs=document.getElementsByTagName("tr");
			var tds1;
			var tds2;
			var result="";
			if(parent.hasScene==0){
				return result;
			}
			for(var i=0;i<trs.length-2;i++){
				tds1=trs[i+2].childNodes;
				if(i==trs.length-3){
					result+=tds1[1].firstChild.value+";";
					return result;
				}else{
					tds2=trs[i+3].childNodes;
				}
				if(tds2[0].childNodes.length==0){
					result+=tds1[1].firstChild.value+",";
				}else{
					result+=tds1[1].firstChild.value+";";
				}
			}
		}
		function getShots(){
			var trs=document.getElementsByTagName("tr");
			var tds1;
			var tds2;
			var result="";
			for(var i=0;i<trs.length-2;i++){
				tds1=trs[i+2].childNodes;
				if(i==trs.length-3){
					result+=tds1[2].firstChild.value+";";
					return result;
				}else{
					tds2=trs[i+3].childNodes;
				}
				if(tds2[0].childNodes.length==0){
					result+=tds1[2].firstChild.value+",";
				}else{
					result+=tds1[2].firstChild.value+";";
				}
			}
		}
		function refresh(){
			window.location.href="ESPform.jsp";
		}
		
		function delEps(id){
			var oTbody=document.getElementById("table1");
			var radioTr=getRowIdFromRadio();
			if(radioTr==null || radioTr=="" || typeof(radioTr)== "undefined"){
				alert("<bean:message key='err.selectepsfirst'/>!");
				return ;
			}
			var radioStr=new String(radioTr);
			var delTr=new Array();
			var tr=document.getElementById(radioStr);
			while(1==1){
				tr=tr.nextSibling;
				if(tr!=null && tr!="" && typeof(tr)!="undefined"){
					if(tr.firstChild.childNodes.length==0){
						delTr.unshift(tr.getAttribute("id"));
					}else{
						break;
					}
				}else{
					break;
				}
			}
			for(var i=0;i<delTr.length;i++){
				oTbody.deleteRow(delTr[i]-1);
			}
			oTbody.deleteRow(radioTr-1);
			resetRowId();
		}
		function delScene(butt){
			var id=butt.parentNode.parentNode.getAttribute("id");
			var oTbody=document.getElementById("table1");
			oTbody.deleteRow(id-1);
			resetRowId();
		}
		function openModel(butt){
			var id=butt.parentNode.parentNode.getAttribute("id");
			window.open("relateModel.jsp?id="+id,"model");
		}
		function isChinese(el){
//			var reg = /^[u4E00-u9FA5]+$/;  中文
			if(isNaN(el.value)){
				alert('<bean:message key="mustInt"/>');
				el.value="";
			}
		}
	</script>
  </head>
  
  <body onload="setHasEsp();setHasScence();setHasShot();">
  	<input type="button" value="<bean:message key='reset'/>" onclick="refresh();">
  	<table id="oTable" border="ffffff">
  	<tbody id="table1">
  		<tr>
  			<td id="addEspButtion">
				<input type="button" value="<bean:message key='episode.add'/>" onclick="addEps();"><input id="addEsps" type="text" value="1">
			</td>
			<td id="addScenceButtion">
				<input type="button" value="<bean:message key='scene.add'/>" onclick="addScene();"><input id="addScenes" type="text" value="1">
			</td>
			<td id="addShotButton">
				<input type="button" value="<bean:message key='shot.add'/>" onclick="addShot();"><input id="addShots" type="text" value="1">
			</td>
   	 	</tr>
   	 	<tr>
   	 		<td><bean:message key='episode'/><input type="button" onclick="delEps();" value="<bean:message key='deleteepisode'/>"></td>
   	 		<td><bean:message key='scene'/></td>
   	 		<td><bean:message key='shotquantity'/></td>
   	 	</tr>
	</tbody>
    </table>
    <span><input type="button" onclick="parent.allSubmit();" value="<bean:message key='createfolders'/>"></span>
  </body>
</html>